package org.skydiveapp;

import java.text.MessageFormat;

import org.skydiveapplibrary.util.CalculatorType;
import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.data.WeightUnit;
import org.skydiveapplibrary.ui.CalculatorTypeAdapter;
import org.skydiveapplibrary.ui.FormatUtility;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class WingLoadCalculatorActivity extends Activity
{
	// data
	private float result = 0;
	
	// settings repository
	private SettingsRepository settingsRepo = new SettingsRepository(this);
	
	// listeners
	private OnItemSelectedListener typeSelectedListener = new OnItemSelectedListener()
	{
		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
		{
			updateUI();
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent)
		{
			updateUI();
		}
	};
	private TextWatcher textChangeListener = new TextWatcher()
	{
		@Override
		public void onTextChanged(CharSequence text, int arg1, int arg2, int arg3)
		{
			// recalculate
			calculate();
		}
		
		@Override
		public void beforeTextChanged(CharSequence text, int arg1, int arg2,
				int arg3)
		{
		}
		
		@Override
		public void afterTextChanged(Editable editable)
		{
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		// initialize UI
		initUI();
		
		// update UI
		updateUI();
	}
	
	private void initUI()
	{
		// show view
		setContentView(R.layout.wing_load_calculator);
			
		// set header
		TextView headerTextView = (TextView)findViewById(R.id.EditHeaderTextView);
		headerTextView.setText(R.string.wing_load_calculator_label);
		
		// load type spinner
		Spinner typeSpinner = (Spinner)findViewById(R.id.WingLoadCalculateTypeSpinner);
		typeSpinner.setAdapter(new CalculatorTypeAdapter(this));
		typeSpinner.setOnItemSelectedListener(typeSelectedListener);
		
		// get weight unit
		WeightUnit weightUnit = settingsRepo.getSettings().getWeightUnit();
		
		// set labels
		TextView yourWeightLabel = (TextView)findViewById(R.id.WingLoadYourWeightLabel);
		yourWeightLabel.setText(getString(R.string.wing_load_your_weight_label));
		
		TextView equipWeightLabel = (TextView)findViewById(R.id.WingLoadEquipmentWeightLabel);
		equipWeightLabel.setText(getString(R.string.wing_load_rig_weight_label));

		// add text change listeners
		EditText yourWeightField = (EditText)findViewById(R.id.WingLoadYourWeightField);
		yourWeightField.setHint(weightUnit.getResourceId());
		yourWeightField.addTextChangedListener(textChangeListener);
		
		EditText equipWeightField = (EditText)findViewById(R.id.WingLoadEquipmentWeightField);
		equipWeightField.setHint(weightUnit.getResourceId());
		equipWeightField.addTextChangedListener(textChangeListener);
		
		EditText canopySizeField = (EditText)findViewById(R.id.WingLoadCanopySizeField);
		canopySizeField.addTextChangedListener(textChangeListener);
		
		EditText wingLoadingField = (EditText)findViewById(R.id.WingLoadDesiredWingLoadingField);
		wingLoadingField.addTextChangedListener(textChangeListener);
	}
	
	private void updateUI()
	{
		// get fields
		Spinner typeSpinner = (Spinner)findViewById(R.id.WingLoadCalculateTypeSpinner);
		TableRow canopySizeRow = (TableRow)findViewById(R.id.WingLoadCanopySizeRow);
		TableRow wingLoadRow = (TableRow)findViewById(R.id.WingLoadDesiredWingLoadingRow);

		// get calculator type
		CalculatorType type = (CalculatorType)typeSpinner.getSelectedItem();
		
		// update based on selected type
		if (type == CalculatorType.WingLoading)
		{
			canopySizeRow.setVisibility(View.VISIBLE);
			wingLoadRow.setVisibility(View.GONE);
		}
		else if (type == CalculatorType.CanopySize)
		{
			canopySizeRow.setVisibility(View.GONE);
			wingLoadRow.setVisibility(View.VISIBLE);
		}
		else if (type == CalculatorType.ExtraWeight)
		{
			canopySizeRow.setVisibility(View.VISIBLE);
			wingLoadRow.setVisibility(View.VISIBLE);
		}
		
		// recalculate
		calculate();
	}
	
	private void calculate()
	{
		// get fields
		Spinner typeSpinner = (Spinner)findViewById(R.id.WingLoadCalculateTypeSpinner);
		EditText yourWeightField = (EditText)findViewById(R.id.WingLoadYourWeightField);
		EditText equipWeightField = (EditText)findViewById(R.id.WingLoadEquipmentWeightField);
		EditText canopySizeField = (EditText)findViewById(R.id.WingLoadCanopySizeField);
		EditText wingLoadingField = (EditText)findViewById(R.id.WingLoadDesiredWingLoadingField);
		TextView resultTextView = (TextView)findViewById(R.id.WingLoadResultField);

		// get calculator type
		CalculatorType type = (CalculatorType)typeSpinner.getSelectedItem();
		
		// get values
		float yourWeight = getValue(yourWeightField);		
		float equipWeight = getValue(equipWeightField);
		float canopySizeSqFt = getValue(canopySizeField);
		float wingLoadLbsSqFt = getValue(wingLoadingField);
		
		// get preferred metrics
		WeightUnit weightUnit = settingsRepo.getSettings().getWeightUnit();

		// calculate result
		this.result = CalculatorType.calculate(
				type,
				yourWeight, equipWeight, weightUnit,
				canopySizeSqFt, wingLoadLbsSqFt);
		
		// get result label format
		String resultStrFormat = getString(R.string.wing_load_result_format);
		String resultUnit = "";
		if (type == CalculatorType.WingLoading)
		{
			resultUnit = getString(R.string.wing_load_unit);
		}
		else if (type == CalculatorType.CanopySize)
		{
			resultUnit = getString(R.string.area_unit_square_feet);
		}
		else if (type == CalculatorType.ExtraWeight)
		{
			resultUnit = getString(weightUnit.getResourceId());
		}
		
		// set result
		resultTextView.setText(MessageFormat.format(resultStrFormat,
				FormatUtility.toNumericString(result),
				resultUnit));
	}
	
	private float getValue(TextView textView)
	{
		String text = textView.getText().toString();
		
		return FormatUtility.toFloat(text, 0);
	}
}
